package com.zline.foodraise.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.zline.foodraise.listener.DynamicListener;
import com.zline.foodraise.listener.RecyclerItemClicked;
import com.zline.foodraise.util.LUtil;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.UiThread;

/**
 * Created by heshantao on 15/10/16.
 * 带头和尾的RecyclerView适配器
 */
@EBean
public abstract class HeaderBottomBaseRecyclerAdapter<T, V extends View> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String LOG_TAG = HeaderBottomBaseRecyclerAdapter.class.getSimpleName();
    @RootContext
    Context cxt;
    final int headerCount=1;//头部View个数
    final int bottomCount = 1;//底部数量
    SparseArray<T> arrys;
    RecyclerItemClicked itemClicked;
    private boolean isFootEnable=false;
    int flag;

    public static enum ITEM_TYPE {
        ITEM_TYPE_HEADER,
        ITEM_TYPE_CONTENT,
        ITEM_TYPE_BOTTOM
    }

    public void setAdapterData(SparseArray<T> sparseArray) {
        this.arrys = sparseArray;
    }

    public void setRecycleItemClickListener(RecyclerItemClicked itemClickListener) {
        this.itemClicked = itemClickListener;
    }


    public void setFootEnable(boolean isFootEnable){
        this.isFootEnable=isFootEnable;
    }
    public boolean getFootEnable(){
        return isFootEnable;
    }
    public void setRecycleFlag(int flag) {
        this.flag = flag;
    }

    public SparseArray<T> getArrys(){
        return arrys;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM_TYPE_CONTENT.ordinal()) {
            LUtil.i(LOG_TAG,"______Content_____");
            return new ContentWrapper<V>(onCreateItemView(parent, viewType));
        } else if (viewType == ITEM_TYPE.ITEM_TYPE_BOTTOM.ordinal()) {
            LUtil.i(LOG_TAG,"______Bottom____");
            return onCreateBottomView(parent, viewType);
        }else{
            LUtil.i(LOG_TAG,"______headerView______");
            return onCreateHeaderView(parent, viewType);
        }
    }

    //创建头部View
    protected abstract HeaderWrapper onCreateHeaderView(ViewGroup parent, int viewType);
    protected abstract V onCreateItemView(ViewGroup parent, int viewType);
    //创建底部View
    protected abstract BottomWrapper onCreateBottomView(ViewGroup parent, int viewType);

    @Override
    public int getItemViewType(int position) {
        LUtil.i(LOG_TAG,"______getItemViewType______"+position);
        if (headerCount != 0 && position < headerCount) {//头部View
            return ITEM_TYPE.ITEM_TYPE_HEADER.ordinal();
        } else if (isFootEnable && position >= (headerCount +arrys.size())) {//底部View
            return ITEM_TYPE.ITEM_TYPE_BOTTOM.ordinal();
        } else {
            return ITEM_TYPE.ITEM_TYPE_CONTENT.ordinal();
        }
    }

    @Override
    public int getItemCount() {
        return isFootEnable ? headerCount+arrys.size() + bottomCount : headerCount+arrys.size();
    }


    static class HeaderWrapper<V extends View> extends RecyclerView.ViewHolder {
        private V view;

        public HeaderWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {
            return view;
        }
    }


    static class ContentWrapper<V extends View> extends RecyclerView.ViewHolder {
        private V view;

        public ContentWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {
            return view;
        }
    }


    static class BottomWrapper<V extends View> extends RecyclerView.ViewHolder {
        private V view;

        public BottomWrapper(V itemView) {
            super(itemView);
            view = itemView;
        }

        public V getView() {
            return view;
        }
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void addAll(SparseArray<T> items) {
        int startIndex = this.arrys.size();
        for(int i=0;i<items.size();i++){
            int key=items.keyAt(i);
            this.arrys.put(startIndex + i, items.get(key));
        }
        notifyItemRangeInserted(startIndex+1, items.size());
    }

    @UiThread(propagation = UiThread.Propagation.REUSE)
    public void clear() {
        int size = arrys.size();
        arrys.clear();
        notifyItemRangeRemoved(0, size);
    }
}
